package com.deyuanyun.pic.ae.controller;

import com.deyuanyun.pic.ae.controller.dto.AEBaseAudit;
import com.deyuanyun.pic.ae.controller.dto.AEPipeLineQuery;
import com.deyuanyun.pic.ae.controller.dto.TrafficFacilitiesEdit;
import com.deyuanyun.pic.ae.controller.vo.TrafficFacilitiesAuditVO;
import com.deyuanyun.pic.ae.controller.vo.TrafficFacilitiesVO;
import com.deyuanyun.pic.ae.domain.TrafficFacilities;
import com.deyuanyun.pic.ae.enumeration.AEExportEnum;
import com.deyuanyun.pic.ae.enumeration.FindTypeEnum;
import com.deyuanyun.pic.ae.service.TrafficFacilitiesService;
import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.converter.BeanConverter;
import com.deyuanyun.pic.common.util.poi.ExcelUtil;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * “交通设施” 管理控制类
 *
 * 方法详情：<br/>
 *
 * @author huanglw
 * @date  2016-11-11
 *
 * History: [修改人][时间][修改描述]
 */
@Controller
@RequestMapping("/ae/trafficFacilities")
public class TrafficFacilitiesController extends AEBaseController {

    @Resource
    private TrafficFacilitiesService trafficFacilitiesService;

    /**
     * 跳转到 “交通设施” 管理页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/index.html")
    public ModelAndView index(ModelMap model){
        return forword("/ae/trafficFacilities", model);
    }

    /**
     * 条件 查询“交通设施”
     *
     * @param aePipeLineQuery
     */
    @RequestMapping("/query.json")
    @ResponseBody
    public void query(AEPipeLineQuery aePipeLineQuery) {
        PageJsonBean jsonBean = new PageJsonBean();
        jsonBean.setList(new TrafficFacilitiesVO().fromDomainList(trafficFacilitiesService.query(aePipeLineQuery)));
        AjaxSupport.sendSuccessText("success", jsonBean);
    }

    /**
     * 条件 导出“交通设施”
     *
     * @param aePipeLineQuery
     */
    @RequestMapping("/export.json")
    @ResponseBody
    public void export(AEPipeLineQuery aePipeLineQuery) throws IOException {

        List<TrafficFacilities> lists = trafficFacilitiesService.query(aePipeLineQuery);
        TrafficFacilitiesVO.ExportObj exportObj = new TrafficFacilitiesVO().fromDomainList(lists);
        List<TrafficFacilitiesVO> postList = exportObj.getPostList();
        List<TrafficFacilitiesVO> lineList = exportObj.getLineList();
        // 交叉
        List<ExcelUtil.SheetParam> list = new ArrayList<ExcelUtil.SheetParam>();
        String[] columnKeys =  {"pipeLineName", "type", "name", "owner", "contact", "telephone",
                "crossMileage", "crossNagm", "crossNagmMileage", "locationDescription", "relativePositionRelation",
                "altitudeSeparation", "safeHeight", "recorder", "recordTime", "remarks"};
        list.add(new ExcelUtil.SheetParam(columnKeys, BeanConverter.toListMap(postList), 2));
        // 平行
        String[] lineColumnKeys = {"pipeLineName", "type", "name", "owner", "contact", "telephone",
                "beginMileage", "beginNagm", "beginNagmMileage", "endMileage", "endNagm", "endNagmMileage",
                "beginDescription", "endDescription", "relativePositionRelation", "minDistance", "parallelLength",
                "safeHeight", "recorder", "recordTime", "remarks"};
        list.add(new ExcelUtil.SheetParam(lineColumnKeys, BeanConverter.toListMap(lineList), 2));

        AEExportEnum exportEnum = AEExportEnum.TrafficFacilities;
        DownExcelUtil.export(exportEnum.getFileName(), aeTemplatePath+exportEnum.getTempPath(), list);
    }

    /**
     * 根据id查询 “交通设施”
     *
     * @param id
     */
    @RequestMapping("/find.json")
    @ResponseBody
    public void find(String id, FindTypeEnum type) throws IOException {
        Asserts.assertLogic(StringUtils.isNullEmpty(id), "交通设施id 不能为空");
        TrafficFacilities trafficFacilities = trafficFacilitiesService.find(id);
        if (FindTypeEnum.edit.equals(type)) {
            TrafficFacilitiesEdit trafficFacilitiesEdit = new TrafficFacilitiesEdit();
            trafficFacilitiesEdit.fromDomain(trafficFacilities);
            AjaxSupport.sendSuccessText("", trafficFacilitiesEdit);
        }
        if (FindTypeEnum.show.equals(type)) {
            TrafficFacilitiesAuditVO trafficFacilitiesVO = new TrafficFacilitiesAuditVO();
            trafficFacilitiesVO.fromDomain(trafficFacilities);
            AjaxSupport.sendSuccessText("", trafficFacilitiesVO);
        }
        AjaxSupport.sendSuccessText("", "类型错误，没有任何数据");
    }

    /**
     * 创建 或者 编辑“交通设施”
     *
     * @param trafficFacilitiesEdit
     */
    @RequestMapping("/edit.json")
    @ResponseBody
    public void edit(@Valid TrafficFacilitiesEdit trafficFacilitiesEdit, Errors errors) {
        int updateRows = trafficFacilitiesService.edit(trafficFacilitiesEdit.toDomain(), trafficFacilitiesEdit.getImgs());
        AjaxSupport.sendSuccessText("success", updateRows);
    }

    /**
     * 审核“交通设施”
     *
     * @param aeBaseAudit
     */
    @RequestMapping("/audit.json")
    @ResponseBody
    public void audit(AEBaseAudit aeBaseAudit) {
        Asserts.assertLogic(StringUtils.isNullEmpty(aeBaseAudit.getId()), "交通设施id 不能为空");
        Asserts.assertLogic(StringUtils.isNullEmpty(aeBaseAudit.getDataStated()), "审核状态 不能为空");
        int updateRows = trafficFacilitiesService.audit(aeBaseAudit.toTrafficFacilities());
        AjaxSupport.sendSuccessText("success", updateRows);
    }

    /**
     * 删除“交通设施”
     *
     * @param id
     */
    @RequestMapping("/delete.json")
    @ResponseBody
    public void delete(String id) {
        int updateRows = trafficFacilitiesService.delete(id);
        AjaxSupport.sendSuccessText("success", updateRows);
    }
}
